package com.aynu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.aynu.gulimall.product.entity.ProductAttrValueEntity;
import com.aynu.gulimall.product.service.ProductAttrValueService;
import com.aynu.gulimall.product.vo.AttrGroupRelationVo;
import com.aynu.gulimall.product.vo.AttrResponseVo;
import com.aynu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.aynu.gulimall.product.entity.AttrEntity;
import com.aynu.gulimall.product.service.AttrService;
import com.aynu.common.utils.PageUtils;
import com.aynu.common.utils.R;



/**
 * 商品属性
 *
 * @author majun
 * @email mjun1833@foxmail.com
 * @date 2020-06-06 21:39:56
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /*
    * 保存规格参数
    * /product/attr/base/list/{catelogId}
    * /product/attr/sale/list/{catelogId}
    * */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId")Long catelogId,
                          @PathVariable("attrType")String type)
    {
        System.out.println(type);
        PageUtils page = attrService.queryBaseAttrPage(params,type,catelogId);
        return R.ok().put("page", page);
    }

    /*
     * spu管理-规格，数据回显
     * /product/attr/base/listforspu/{spuId}
     * */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId)
    {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data", entities);
    }

    /*
     * spu管理-规格-修改，保存修改
     * /product/attr/update/{spuId}
     * */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities)
    {
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /*
    * /product/attrgroup/attr/relation/delete
    * 属性分组-关联-移出
    * */

    //@PostMapping("/attr/relation/delete")
    public R attrRelationDelete(AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }


    /**
     * 信息
     * /product/attr/info/{attrId}
     * 修改信息的回显
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);

        AttrResponseVo attrResponseVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     * AttrVo,用于收集前端传过来的数据
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
//		attrService.updateById(attr);
        attrService.updateAttr(attrVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
